package com.nwpusp.display.views;

import java.util.ArrayList;

import com.nwpusp.display.GlobalVar;
import com.nwpusp.display.MainActivity;
import com.nwpusp.display.views.components.BackGround;
import com.nwpusp.display.views.components.Button;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class CreateView extends SurfaceView implements SurfaceHolder.Callback{
	
	private MainActivity mainActivity;
	private CreateViewDrawThread myCreateViewDrawThread = null;
	private ArrayList<Button> al_Btn = new ArrayList<Button>();
	private Button btn_CreateGame;
	private Button btn_JoinGame;
	private Button btn_Back;
	private BackGround createViewBackGround;
	private Paint myPaint;
	private float btn_CreateGame_X;
	private float btn_CreateGame_Y;
	private float btn_JoinGame_X;
	private float btn_JoinGame_Y;
	private float btn_Back_X;
	private float btn_Back_Y;
	private float createViewBackGround_X;
	private float createViewBackGround_Y;
	private float btn_Scale = (float)0.2;
	
	public CreateView(MainActivity mainActivity){
		super(mainActivity);
		this.mainActivity = mainActivity;
		myPaint = new Paint();
		btn_CreateGame = new Button(mainActivity,btn_Scale,GlobalVar.CREATE_GAME);
		btn_JoinGame = new Button(mainActivity,btn_Scale,GlobalVar.JOIN_GAME);
		btn_Back = new Button(mainActivity,btn_Scale,GlobalVar.BACK);
		createViewBackGround = new BackGround(mainActivity, 1, GlobalVar.CreateViewBackGround);
		caculateLocation();
		btn_CreateGame.setLocation(btn_CreateGame_X,btn_CreateGame_Y);
		btn_JoinGame.setLocation(btn_JoinGame_X,btn_JoinGame_Y);
		btn_Back.setLocation(btn_Back_X,btn_Back_Y);
		createViewBackGround.setLocation(createViewBackGround_X,createViewBackGround_Y);
		al_Btn.add(btn_CreateGame);
		al_Btn.add(btn_JoinGame);
		al_Btn.add(btn_Back);
		myCreateViewDrawThread = new CreateViewDrawThread(this, getHolder());
		getHolder().addCallback(this);
	}
	
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		canvas.drawBitmap(createViewBackGround.draw(),createViewBackGround.x ,createViewBackGround.y,myPaint);
		canvas.drawBitmap(btn_CreateGame.draw(),btn_CreateGame.x ,btn_CreateGame.y,myPaint);
		canvas.drawBitmap(btn_JoinGame.draw(), btn_JoinGame.x,  btn_JoinGame.y,myPaint);
		canvas.drawBitmap(btn_Back.draw(),btn_Back.x ,btn_Back.y,myPaint);
	}
	
	public boolean onTouchEvent(MotionEvent event) {
		float x = event.getX();
		float y = event.getY();
		for(Button btn :al_Btn){
			if(btn.isClick(x,y)){
				mainActivity.mhadnler.sendEmptyMessage(btn.btnIndex);
			}
		}
		return super.onTouchEvent(event);
	}
	
	private void caculateLocation(){
		createViewBackGround_X = 0;
		createViewBackGround_Y = 0;
		btn_Back_X = 0;
		btn_Back_Y = 0;
		btn_CreateGame_X = (GlobalVar.screenWidth - btn_CreateGame.width)*GlobalVar.half;
		btn_CreateGame_Y = GlobalVar.screenHeight*GlobalVar.one_third - btn_CreateGame.height*GlobalVar.half;
		btn_JoinGame_X =  (GlobalVar.screenWidth - btn_JoinGame.width)*GlobalVar.half;
		btn_JoinGame_Y = GlobalVar.screenHeight*GlobalVar.one_third*GlobalVar.twice - btn_JoinGame.height*GlobalVar.half;
	}
	
	public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {
		myCreateViewDrawThread.setFlag(true);
		myCreateViewDrawThread.start();	
	}
	public void surfaceCreated(SurfaceHolder holder) {
	
	}
	public void surfaceDestroyed(SurfaceHolder holder) {
		 boolean retry = true;
		 myCreateViewDrawThread.setFlag(false);
	        while (retry) {
	            try {
	            	myCreateViewDrawThread.join();
	                retry = false;
	            } 
	            catch (InterruptedException e) {
	            }
	        }
	}
}
